package com.maserb.monitor.remote.impl;

import com.maserb.monitor.entity.card.CardPackageEntity;
import com.maserb.monitor.entity.card.CardRecordEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentEntity;
import com.maserb.monitor.remote.BrushCardService;
import com.maserb.monitor.repository.schoolStudent.SchoolStudentRepository;
import com.maserb.monitor.repository.schoolStudent.StudentAttendanceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.jws.WebService;
import java.util.List;

@WebService(targetNamespace = "http://remote.monitor.maserb.com/", endpointInterface = "com.maserb.monitor.remote.BrushCardService")
public class BrushCardServiceImpl implements BrushCardService {

    @Autowired
    private SchoolStudentRepository studentRepository;

    @Autowired
    private StudentAttendanceRepository attendanceRepository;

    @Override
    public String acceptBrushCard(String content) {
        CardPackageEntity entity = CardPackageEntity.convert(content);
        List<CardRecordEntity> recordList = entity.getRecords();

        for(CardRecordEntity r : recordList) {
            if(!StringUtils.isEmpty(r.getCardNumber())) {
                SchoolStudentEntity student = studentRepository.findFirstByCardNumber(r.getCardNumber());
                r.setStudent(student);

                attendanceRepository.save(r);
            }
        }

        return "received:" + content;
    }
}
